﻿using System;
using System.Windows.Forms;

namespace SedionFormDesigner.CusControls
{
    public class ExternPropTextBoxEditingControl : System.Windows.Forms.TextBox, IDataGridViewEditingControl
    {
        #region 私有字段

        protected Int32 rowIndex;
        protected System.Windows.Forms.DataGridView grdView;
        protected Boolean valueChanged = false;

        #endregion

        #region 事件

        /// <summary>
        ///  判断文本是否被编辑过
        /// </summary>
        /// <param name="e"></param>
        protected override void OnTextChanged(EventArgs e)
        {
            valueChanged = true;
            this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
            base.OnTextChanged(e);
        }

        #region IDataGridViewEditingControl 成员

        /// <summary>
        ///  设置单元格样式
        /// </summary>
        /// <param name="dataGridViewCellStyle"></param>
        public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
        {
            this.Font = grdView.Font;
            this.ForeColor = dataGridViewCellStyle.ForeColor;
            this.BackColor = dataGridViewCellStyle.BackColor;
        }

        /// <summary>
        ///  设置当前的DataGridView
        /// </summary>
        public System.Windows.Forms.DataGridView EditingControlDataGridView
        {
            get
            {
                return grdView;
            }
            set
            {
                grdView = value;
            }
        }

        /// <summary>
        ///  获取或设置编辑器正在修改的单元格的格式化值
        /// </summary>
        public object EditingControlFormattedValue
        {
            get
            {
                return this.Text.ToString();
            }
            set
            {
                var text = value as String;
                if (text != null)
                    this.Text = text;
            }
        }

        /// <summary>
        ///  设置处于编辑状态的DataGridView控件的行索引
        /// </summary>
        public int EditingControlRowIndex
        {
            get
            {
                return rowIndex;
            }
            set
            {
                rowIndex = value;
            }
        }

        /// <summary>
        ///  获取或者设置控件值是否发生变化
        /// </summary>
        public bool EditingControlValueChanged
        {
            get
            {
                return valueChanged;
            }
            set
            {
                valueChanged = value;
            }
        }

        /// <summary>
        ///  确定指定的键是应由编辑控件处理的常规输入还是应由DataGridView处理的特殊键
        /// </summary>
        /// <param name="keyData"></param>
        /// <param name="dataGridViewWantsInputKey"></param>
        /// <returns></returns>
        public bool EditingControlWantsInputKey(Keys keyData, bool dataGridViewWantsInputKey)
        {
            switch (keyData & Keys.KeyCode)
            {
                case Keys.Left:
                case Keys.U:
                case Keys.Down:
                case Keys.Right:
                case Keys.Home:
                case Keys.End:
                case Keys.PageDown:
                case Keys.PageUp:
                    return true;
                default:
                    return false;
            }
        }

        /// <summary>
        ///  取得光标样式
        /// </summary>
        public Cursor EditingPanelCursor
        {
            get { return base.Cursor; }
        }

        /// <summary>
        ///  检索单元格的格式化值
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
        {
            return EditingControlFormattedValue;
        }

        /// <summary>
        ///  准备控件及其内容以进行编辑
        /// </summary>
        /// <param name="selectAll"></param>
        public void PrepareEditingControlForEdit(bool selectAll)
        {

        }

        /// <summary>
        ///  确定值每当值变更时是否需要重新定位存储格内容
        /// </summary>
        public bool RepositionEditingControlOnValueChange
        {
            get { return false; }
        }

        #endregion

        #endregion
    }
}
